package com.xianggg.xservers.weixin.common;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.util.HashMap;
import java.util.Map;

public class AjaxJson {
    public boolean success;
    public String msg;
    public Object obj;
    public Map<String, Object> attribute;

    public static AjaxJson success(Object obj) {
        AjaxJson json = new AjaxJson();
        json.success = true;
        json.obj = obj;
        return json;
    }

    public static AjaxJson error(String msg) {
        AjaxJson json = new AjaxJson();
        json.success = false;
        json.msg = msg;
        return json;
    }
    public static AjaxJson error(String msg,String code) {
        AjaxJson json = new AjaxJson();
        json.success = false;
        json.msg = msg;
        json.obj = code;
        return json;
    }

    public String toString(){
        Gson gson = new GsonBuilder().serializeNulls().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
        return gson.toJson(this);
    }

    public AjaxJson() {
    }

    public AjaxJson(boolean success, Object obj, String msg) {
        this.success = success;
        this.msg = msg;
        this.obj = obj;
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getObj() {
        return obj;
    }

    public void setObj(Object obj) {
        this.obj = obj;
    }

    public Map<String, Object> getAttribute() {
        if (attribute == null) {
            attribute = new HashMap<>();
        }
        return attribute;
    }

    public void setAttribute(Map<String, Object> attribute) {
        this.attribute = attribute;
    }
}
